<?php

class Weibo_model extends CI_Model {
    
    private $_table = 'weibo';
    
    public function __construct() {
        parent::__construct();
    }
    
    function create($data) {
        $id_md5 = md5($data['mid']);
        $b = $this->get_weibo_by_id($id_md5);
        
        if(empty($b) && !empty($data['text'])){
            $post = array(
                'content' => json_encode($data),
                'id_md5' => $id_md5,
                'created' => date('Y-m-d H:i:s', strtotime($data['created_at']))
            );
            
            $this->db->insert($this->_table, $post);
            return $this->db->insert_id();
        }
    }
    
    public function get_weibo_by_id($ID) {
        $query = $this->db->get_where($this->_table, array('id_md5' => $ID))->row();
        
        return $query;
    }
    
    public function get_weibo($num, $offset, $where = null) {
        $this->db->order_by("created", "desc");
        $query = $this->db->get($this->_table, $num, $offset);
        $data = $query->result();
        foreach($data as $v)
        {
            $tmp[] = json_decode($v->content);
        }
        
        return $tmp;
    }
    
    public function get_count_all() {
        $this->db->select('COUNT(*) AS count');
        $result =  $this->db->get($this->_table)->row();

        return $result->count;
    }
    
    public function weibo_sync($data)
    {
        $result = FALSE;
        $message = '';
        $i = 0;
        
        foreach($data as $v)
        {
            $weibo_id = $this->create($v);
            $result = TRUE;
            
            if($weibo_id)
            {
                $i ++;
                
                if (isset($v['retweeted_status']))
                {
                    $message .= '
                        <li id="'. $v['mid'] .'" >
                            <div class="grid_1">
                                <img src=" '. $v['user']['profile_image_url'] . ' ">
                            </div>
                            <div class="prefix_1">
                                <p><strong>'. $v['user']['screen_name'] .'</strong></p>
                                <p>'. $v['text'] .'</p>
                                <p>'.date('Y-m-d H:i:s', strtotime($v['created_at'])).'</p>
                            </div>';
                    
                    $message .= '
                            <div class="triangle"></div>
                            <blockquote>
                                <div class="grid_1">
                                    <img src=" '. $v['retweeted_status']['user']['profile_image_url'] . ' ">
                                </div>
                                <div class="prefix_1">
                                    <p><strong>'. $v['retweeted_status']['user']['screen_name'] .'</strong></p>
                                    <p>'. $v['retweeted_status']['text'] .'</p>
                                    <p>'.date('Y-m-d H:i:s', strtotime($v['retweeted_status']['created_at'])).'</p>';

                                    if(isset($v['retweeted_status']['thumbnail_pic']))
                                    {
                                        $message .= '<img src="'. $v['retweeted_status']['thumbnail_pic'] .'" alt="转发图片"/>';
                                    }

                        $message .= '
                                </div>
                            </blockquote>';
                    
                    $message .= '
                        </li>';
                }
                else
                {
                    $message .= '
                        <li id="'. $v['mid'] .'" >';
                    $message .= '
                            <blockquote>
                                <div class="grid_1">
                                    <img src=" '. $v['user']['profile_image_url'] . ' ">
                                </div>
                                <div class="prefix_1">
                                    <p><strong>'. $v['user']['screen_name'] .'</strong></p>
                                    <p>'. $v['text'] .'</p>
                                    <p>'.date('Y-m-d H:i:s', strtotime($v['created_at'])).'</p>';

                                    if(isset($v['thumbnail_pic']))
                                    {
                                        $message .= '<img src="'. $v['thumbnail_pic'] .'" alt="转发图片"/>';
                                    }
                        
                    $message .= '</div>
                            </blockquote>
                        </li>';
                }
            }
        }
        $message = '<ol>'.$message .'</ol>';
        
        return json_encode(array('result'=>$result, 'message' => $message, 'count' => $i));
    }
    
    function weibo_item_html($data)
    {
        $message = '<div class="weibo_con"><strong>'.$data->user->name.' : </strong>'.$data->text.'</div>';
        
        
        if(isset($data->retweeted_status))
        {
            $message .= '
            <div class="grid_1">
                <img src="'.$data->retweeted_status->user->profile_image_url.'">
            </div>

            <div class="prefix_1">
                <p><strong>'.$data->retweeted_status->user->name.'</strong></p>
                <p>'.$data->retweeted_status->text.'</p>
                <p class="weibo_item_created">'.date('Y-m-d H:i:s', strtotime($data->retweeted_status->created_at)) .'</p>
            </div>';
            if(isset($data->retweeted_status->bmiddle_pic))
            {
                $message .= '<div class="weibo_item_img"><img src="'. $data->retweeted_status->bmiddle_pic .'" alt="转发图片"/></div>';
            }
        }
        
        return $message;
    }
    
    function weibo_item_comment_html($data)
    {
        $message = '';
        foreach($data as $v)
        {
            $message .= '
                <div class="weibo_item_comment">
                    <div class="grid_1">
                        <img src=" '. $v['user']['profile_image_url'] . ' ">
                    </div>
                    <div class="prefix_1">
                        <p><strong>'.  $v['user']['name'] .'</strong></p>
                        <p>'.  $v['text'].'</p>
                        <p class="weibo_item_created">'.date('Y-m-d H:i:s', strtotime($v['created_at'])).'</p>
                    </div>
                </div>
            ';
        }
        
        return $message;
    }
}